<template>
  <div class="login">
    <el-row style="margin-top: 260px;z-index: 1;">
      <el-col :offset="9" :span="5">
        <el-card v-loading="loading" class="login-box" element-loading-text="正在验证..." element-loading-background="rgba(0, 0, 0, 0.8)">
          <el-form :model="user" :rules="loginRules" ref="loginForm" status-icon>
            <h1 class="title">ZOE管理系统</h1>
            <p class="login-box-msg">欢迎使用</p>
            <el-form-item prop="username">
              <el-input type="text" v-model="user.username" auto-complete="off" placeholder="请输入用户名..." suffix-icon="fa fa-user"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" v-model="user.password" auto-complete="off" placeholder="请输入密码..." suffix-icon="fa fa-lock"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="login" class="pull-right">登录</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<style scoped>
.login {
  background: url(../assets/bg.jpg) no-repeat scroll center center / cover;
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
  position: fixed;
}

.login-box {
  border-color: rgba(0, 0, 0, 0.5);
  background: rgba(0, 0, 0, 0.5);
}

.login-box-msg {
  color: #ffffff;
  text-align: center;
}

.login-box .title {
  color: #ffffff;
  text-align: center;
}
</style>
<script>
export default {
  data() {
    return {
      loading: false,
      user: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('Login', this.user)
            .then(() => {
              this.loading = false
              this.$router.push({ path: '/' })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
